package com.ruoyi.puyu.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.puyu.common.MyTableDataInfo;
import com.ruoyi.puyu.domain.PuyuLesson;
import com.ruoyi.puyu.domain.PuyuLessonStudent;
import com.ruoyi.puyu.service.IPuyuLessonService;
import com.ruoyi.puyu.service.IPuyuLessonStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课时Controller
 * 
 * @author wusongwen
 * @date 2024-05-01
 */
@RestController
@RequestMapping("/puyu/lesson")
public class PuyuLessonController extends BaseController
{
    @Autowired
    private IPuyuLessonService puyuLessonService;
    @Autowired
    private IPuyuLessonStudentService puyuLessonStudentService;

    /**
     * 查询课时列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:lesson:list')")
    @GetMapping("/list")
    public MyTableDataInfo list(PuyuLesson puyuLesson)
    {
        puyuLesson.setDelFlag("0");
        boolean bolRole = SecurityUtils.hasRole(UserConstants.ROLE_COACH)
                || SecurityUtils.hasRole(UserConstants.ROLE_STUDENT)
                || SecurityUtils.hasRole(UserConstants.ROLE_PARENT);
        if (bolRole) {
            puyuLesson.setUserId(SecurityUtils.getUserId());
        }
        bolRole = SecurityUtils.hasRole(UserConstants.ROLE_MANAGER)
                || SecurityUtils.hasRole(UserConstants.ROLE_ADMIN);
        if (bolRole) {
            puyuLesson.setUserId(null);
        }
        MyTableDataInfo dataTable = new MyTableDataInfo();
        startPage();
        List<PuyuLesson> list = puyuLessonService.selectPuyuLessonList(puyuLesson);
        TableDataInfo dataTableInfo = getDataTable(list);
        Integer pageNum = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        // 分页第一个页，查询课时学员总数
        if (pageNum == 1 && !list.isEmpty()) {
            // 查询课时学员总数
            int lessonStudentNumber = puyuLessonService.selectPuyuLessonStudentTotal(puyuLesson);
            dataTable.setExtTotal(lessonStudentNumber);
        } else {
            dataTable.setExtTotal(-1);
        }
        dataTable.setCode(dataTableInfo.getCode());
        dataTable.setMsg(dataTableInfo.getMsg());
        dataTable.setRows(dataTableInfo.getRows());
        dataTable.setTotal(dataTableInfo.getTotal());
        return dataTable;
    }

    /**
     * 查询课时列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:lesson:list')")
    @GetMapping("/getStudentTotal")
    public int getStudentTotal(PuyuLesson puyuLesson)
    {
        puyuLesson.setDelFlag("0");
        boolean bolRole = SecurityUtils.hasRole(UserConstants.ROLE_COACH)
                || SecurityUtils.hasRole(UserConstants.ROLE_STUDENT)
                || SecurityUtils.hasRole(UserConstants.ROLE_PARENT);
        if (bolRole) {
            puyuLesson.setUserId(SecurityUtils.getUserId());
        }
        bolRole = SecurityUtils.hasRole(UserConstants.ROLE_MANAGER)
                || SecurityUtils.hasRole(UserConstants.ROLE_ADMIN);
        if (bolRole) {
            puyuLesson.setUserId(null);
        }
        List<PuyuLesson> list = puyuLessonService.selectPuyuLessonList(puyuLesson);
        // 总共学员人数
        int lessonStudentNumber = 0;
        if (null == list || list.isEmpty()) {
            return  lessonStudentNumber;
        }

        for(PuyuLesson puyuLessonTemp:list) {
            if (null != puyuLessonTemp.getPuyuLessonStudents() && !puyuLessonTemp.getPuyuLessonStudents().isEmpty()) {
                lessonStudentNumber += puyuLessonTemp.getPuyuLessonStudents().size();
            }
        }

        return lessonStudentNumber;
    }

    /**
     * 查询课时列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:lesson:list')")
    @GetMapping("/listStudent")
    public TableDataInfo listStudent(PuyuLessonStudent puyuLessonStudent)
    {
        puyuLessonStudent.setDelFlag(0);
        startPage();
        List<PuyuLessonStudent> list = puyuLessonStudentService.selectPuyuLessonStudentList(puyuLessonStudent);
        return getDataTable(list);
    }

    /**
     * 导出课时列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:lesson:export')")
    @Log(title = "导出课时", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PuyuLesson puyuLesson)
    {
        List<PuyuLesson> list = puyuLessonService.selectPuyuLessonList(puyuLesson);
        ExcelUtil<PuyuLesson> util = new ExcelUtil<PuyuLesson>(PuyuLesson.class);
        util.exportExcel(response, list, "课时数据");
    }

    /**
     * 获取课时详细信息
     */
    @PreAuthorize("@ss.hasPermi('puyu:lesson:query')")
    @GetMapping(value = "/{lessonId}")
    public AjaxResult getInfo(@PathVariable("lessonId") Long lessonId)
    {
        return success(puyuLessonService.selectPuyuLessonByLessonId(lessonId));
    }

    /**
     * 新增课时
     */
    @PreAuthorize("@ss.hasPermi('puyu:lesson:add')")
    @RepeatSubmit(interval = 600000, message = "请求过于频繁")
    @Log(title = "添加课时", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PuyuLesson puyuLesson)
    {
        return toAjax(puyuLessonService.insertPuyuLesson(puyuLesson));
    }

    /**
     * 修改课时
     */
    @PreAuthorize("@ss.hasPermi('puyu:lesson:edit')")
    @Log(title = "修改课时", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PuyuLesson puyuLesson)
    {
        return toAjax(puyuLessonService.updatePuyuLesson(puyuLesson));
    }

    /**
     * 删除课时
     */
    @PreAuthorize("@ss.hasPermi('puyu:lesson:remove')")
    @Log(title = "删除课时", businessType = BusinessType.DELETE)
	@DeleteMapping("/{lessonIds}")
    public AjaxResult remove(@PathVariable Long[] lessonIds)
    {
        return toAjax(puyuLessonService.deletePuyuLessonByLessonIds(lessonIds));
    }

    /**
     * 获取学员课时点名详细信息
     */
    @PreAuthorize("@ss.hasPermi('puyu:lesson:queryStudent')")
    @GetMapping(value = "/student/{lessonStudentId}")
    public AjaxResult getStudent(@PathVariable("lessonStudentId") Long lessonStudentId)
    {
        return success(puyuLessonStudentService.selectPuyuLessonStudentByLessonStudentId(lessonStudentId));
    }

    /**
     * 新增学员课时点名
     */
    @PreAuthorize("@ss.hasPermi('puyu:lessons:addStudent')")
    @Log(title = "学员点名新增", businessType = BusinessType.INSERT)
    @PostMapping("/student")
    public AjaxResult addStudent(@RequestBody PuyuLessonStudent puyuLessonStudent)
    {
        return toAjax(puyuLessonStudentService.insertPuyuLessonStudent(puyuLessonStudent));
    }

    /**
     * 修改学员课时点名
     */
    @PreAuthorize("@ss.hasPermi('puyu:lesson:editStudent')")
    @Log(title = "学员点名编辑", businessType = BusinessType.UPDATE)
    @PutMapping("/student")
    public AjaxResult editStudent(@RequestBody PuyuLessonStudent puyuLessonStudent)
    {
        return toAjax(puyuLessonStudentService.updatePuyuLessonStudent(puyuLessonStudent));
    }

    /**
     * 删除学员课时点名
     */
    @PreAuthorize("@ss.hasPermi('puyu:lesson:removeStudent')")
    @Log(title = "学员点名删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/student/{lessonStudentIds}")
    public AjaxResult removeStudent(@PathVariable Long[] lessonStudentIds)
    {
        return toAjax(puyuLessonStudentService.deletePuyuLessonStudentByLessonStudentIds(lessonStudentIds));
    }
}
